找传奇、传世资源到传世资源站!

android 加载网络轮播图片 示例源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboardpackage com.example.downloadpictures;import java.util.ArrayList;import java.util.List;import java.util.Timer;import java.util.TimerTask;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;import android.app.Activity;import android.content.pm.ActivityInfo;import android.graphics.Bitmap;import android.os.AsyncTask;import android.os.Bundle;import android.os.Handler;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.support.v4.view.ViewPager.OnPageChangeListener;import android.util.Log;import android.view.KeyEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.Button;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.LinearLayout.LayoutParams;import android.widget.TextView;import android.widget.Toast;import com.example.downloadpictures.object.WebServiceItem;import com.example.downloadpictures.utils.Utils;public class MainActivity extends Activity {private final String HOST_ADDRESS = "http://122.114.20.230:8888";private final String RESOURCE_ADDRESS = "/DiaoLan/android/ad/list.jsp";private final String SERVER_ADDRESS = HOST_ADDRESS RESOURCE_ADDRESS;private boolean loopPlayState = false;private List<WebServiceItem> mData = null;private List<ImageView> mImageViewList = null;private List<View> mViewList = null;private TextView mArticleTitle = null;private ViewPager mViewPager = null;private MyPagerAdapter adapter = null;private WebView mWebView;// webView调用private LinearLayout mCustomSpace = null;private Handler mHandler = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);this.initWidget();}private void initWidget() {// TODO Auto-generated method stubmViewPager = (ViewPager) this.findViewById(R.id.viewpager);mArticleTitle = (TextView) this.findViewById(R.id.article_title);mCustomSpace = (LinearLayout) this.findViewById(R.id.custom_space);mHandler = new Handler();mData = new ArrayList<WebServiceItem>();mImageViewList = new ArrayList<ImageView>();mViewList = new ArrayList<View>();adapter = new MyPagerAdapter();loadData();mViewPager.setAdapter(adapter);// 设置一个监听器,当ViewPager中的页面改变时调用mViewPager.setOnPageChangeListener(new MyPageChangeListener());}private void loadData() {// TODO Auto-generated method stubnew DownLoad(null).execute(SERVER_ADDRESS);}private final class ClickListener implements OnClickListener {private int mPosition;public ClickListener() {// TODO Auto-generated constructor stub}public ClickListener(int mPosition) {this.mPosition = mPosition;}@Overridepublic void onClick(View v) {if (v instanceof ImageView) {String url = mData.get(mPosition).getmArticleUrl();Toast.makeText(MainActivity.this, url, 1).show();} else {return;}}}private final class MyPagerAdapter extends PagerAdapter {@Overridepublic int getCount() {// TODO Auto-generated method stubreturn mImageViewList.size();}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {// TODO Auto-generated method stub((ViewPager) container).removeView(mImageViewList.get(position));}@Overridepublic Object instantiateItem(ViewGroup container, int position) {// TODO Auto-generated method stubImageView temp = mImageViewList.get(position);new DownLoad(temp).execute(mData.get(position).getmPictureUrl());((ViewPager) container).addView(temp);return temp;}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {// TODO Auto-generated method stubreturn arg0 == arg1;}}private final class DownLoad extends AsyncTask<String, Void, Bitmap> {private ImageView mImageView;public DownLoad(ImageView imageView) {mImageView = imageView;}@Overrideprotected Bitmap doInBackground(String... params) {// TODO Auto-generated method stubtry {if (params[0].equals(SERVER_ADDRESS)) {String json = (String) (Utils.getData(params[0],String.class));mData = Utils.fromJson(json);mImageViewList.clear();Log.e("图片地址", mData.get(0).getmPictureUrl());Log.e("还原", Utils.toJson(mData));} else {Bitmap bitmap = (Bitmap) (Utils.getData(params[0],Bitmap.class));return bitmap;}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}@Overrideprotected void onPostExecute(Bitmap result) {// TODO Auto-generated method stubsuper.onPostExecute(result);if (result != null) {mImageView.setImageBitmap(result);if (!loopPlayState) {mArticleTitle.setText(mData.get(0).getmTitle());mViewPager.setCurrentItem(0);mHandler.postDelayed(loopPlay, 3000);loopPlayState = true;}} else {for (int i = 0; i < mData.size(); i ) {Log.e("JRSEN-MDATA", mData.size() "");ImageView imageView = new ImageView(MainActivity.this);imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));imageView.setOnClickListener(new ClickListener(i));mImageViewList.add(imageView);View view = new View(MainActivity.this);LayoutParams layoutParams = new LayoutParams(14, 14);layoutParams.setMargins(3, 0, 3, 0);view.setLayoutParams(layoutParams);view.setBackgroundResource(R.drawable.dot_normal);mCustomSpace.addView(view);mViewList.add(view);}adapter.notifyDataSetChanged();}}}/** * 当ViewPager中页面的状态发生改变时调用 * * @author Administrator * */private class MyPageChangeListener implements OnPageChangeListener {private int historyPosition = 0;/** * 当ViewPager中页面的状态发生改变时调用 */public void onPageSelected(int position) {mArticleTitle.setText(mData.get(position).getmTitle());mViewList.get(historyPosition).setBackgroundResource(R.drawable.dot_normal);mViewList.get(position).setBackgroundResource(R.drawable.dot_focused);historyPosition = position;}public void onPageScrollStateChanged(int arg0) {}public void onPageScrolled(int arg0, float arg1, int arg2) {}}Runnable loopPlay = new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubint position = mViewPager.getCurrentItem();if (position == (mData.size() - 1)) {position = 0;mViewPager.setCurrentItem(position);} else {mViewPager.setCurrentItem( position);}P.error("position thread" position "mData" mData.size());mHandler.postDelayed(loopPlay, 3000);}};/** * 该类为测试类 * * @author Jrsen * */private final static class P {public static void error(Object str) {Log.e("JRSEN", (String) str);}}}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复